package com.csdj.controller;


import com.csdj.common.utils.ResponseResult;
import com.csdj.dto.LoginDto;
import com.csdj.service.SyEmpService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author 涵哥
 * @date 2023-08-25 8:50
 */
@Controller
@RequestMapping("/autoWater")
public class SyLoginController {
    @Autowired
    private SyEmpService syEmpService;

    /**
     * 登录功能
     * @param loginDto
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestBody LoginDto loginDto){
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(loginDto.getEmail(),loginDto.getPassword()));
            return ResponseResult.okResult(syEmpService.login(loginDto.getEmail()));
        } catch (AuthenticationException e) {
            return ResponseResult.errorResult(500,"服务器正在忙碌中...");
        }
    }
    @RequestMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "redirect:/login.html";
    }
    /**
     * 修改密码
     * @param loginDto
     * @return
     */
    @PutMapping("/updatePwd")
    @ResponseBody
    public ResponseResult updatePwd(@RequestBody LoginDto loginDto){
        return syEmpService.updatePwd(loginDto);
    }
}
